﻿using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using WebAPI.Common.Tools;
using 控制台测试.Service;


//UserService.SayHello(); 
//UserService.SayHello();

//UserService2 instance1 = UserService2.Instance;
//UserService2 instance2 = UserService2.Instance;


//Console.WriteLine(instance1 == instance2);
//Console.WriteLine(instance1.GetHashCode().ToString());
//Console.WriteLine(instance2.GetHashCode().ToString());


//UserService3 instance1 = UserService3.GetInstance();
//UserService3 instance2 = UserService3.GetInstance();
//UserService3 instance3 = UserService3.GetInstance();

//instance1.ShowHashCode();
//instance2.ShowHashCode();
//instance3.ShowHashCode();

//for (int i = 0; i < 100; i++)
//{
//    Task.Run(() =>
//    {
//        UserService3.GetInstance().ShowHashCode();
//    });
//}



//Semaphore semaphore = new Semaphore(3, 3);
//for (int i = 0; i < 100; i++)
//{
//    int n = i;
//    Task.Run(() =>
//    {
//        semaphore.WaitOne();
//        Console.WriteLine($"count: {n % 3 + 1} do something !");
//        semaphore.Release();
//    });
//}

//Console.ReadLine();

//string rawStr = "HelloWorld";
//string encryptedStr = EncryptHelper.DESCrypt(rawStr);
//string decryptedStr = EncryptHelper.DESDecrypt(encryptedStr);

//Console.WriteLine(encryptedStr);
//Console.WriteLine(decryptedStr);

/*
RSA rsa = RSA.Create();

RSAParameters publicKey = rsa.ExportParameters(false);
RSAParameters privateKey = rsa.ExportParameters(true);


string content = "abc123456";

byte[] encryptBytes = RSAEncrypt(content, publicKey);


byte[] rawBytes = RSADecrypt(encryptBytes, privateKey);
Console.WriteLine(Encoding.UTF8.GetString(rawBytes));


byte[] RSAEncrypt(string content,RSAParameters encryptKey)
{
    using(RSA rsa = RSA.Create())
    {
        rsa.ImportParameters(encryptKey);
        return rsa.Encrypt(Encoding.UTF8.GetBytes(content),RSAEncryptionPadding.Pkcs1);
    }
}

byte[] RSADecrypt(byte[] data,RSAParameters decryptKey)
{
    using(RSA rsa = RSA.Create())
    {
        rsa.ImportParameters(decryptKey);
        return rsa.Decrypt(data,RSAEncryptionPadding.Pkcs1);
    }
}
*/

/*
RSA rsa = new RSACryptoServiceProvider();
string? pubKey = Convert.ToBase64String(rsa.ExportRSAPublicKey());
string? priKey = Convert.ToBase64String(rsa.ExportRSAPrivateKey());

string content = "admin123456";

byte[] encryptData = RSAEncrypt(content, pubKey);

byte[] rawBytes = RSADecrypt(encryptData, priKey);
Console.WriteLine(Encoding.UTF8.GetString(rawBytes));




byte[] RSAEncrypt(string content, string encryptKey)
{
    using (RSA rsa = new RSACryptoServiceProvider())
    {
        rsa.ImportRSAPublicKey(Convert.FromBase64String(encryptKey),out _); 
        return rsa.Encrypt(Encoding.UTF8.GetBytes(content), RSAEncryptionPadding.Pkcs1);
    }
}

byte[] RSADecrypt(byte[] data, string decryptKey)
{
    using (RSA rsa = RSA.Create())
    {
        rsa.ImportRSAPrivateKey(Convert.FromBase64String(decryptKey), out _);
        return rsa.Decrypt(data, RSAEncryptionPadding.Pkcs1);
    }
}
*/

/*
Type[] types = typeof(Program).Assembly.GetTypes();
List<Type> typeList = types.ToList();

List<Type> classTypeList = typeList.FindAll(t=>t.IsClass);
//List<Type> interfaceTypeList = typeList.FindAll(t=>t.IsInterface);

Dictionary<Type, Type> implMapping = new Dictionary<Type,Type>();
Dictionary<Type, Type> finalMapping = new Dictionary<Type,Type>();

foreach (Type classType in classTypeList)
{
    Type[] interfaceList = classType.GetInterfaces();

    foreach (Type interfaceType in interfaceList)
    {
        implMapping.Add(interfaceType, classType);
    }
}


foreach (var item in implMapping)
{
    Console.WriteLine($"接口名称: {item.Key.Name},类名: {item.Value.Name}");
}
*/

int count = 0;
object lockObj = new object();

for (int i = 0; i < 10; i++)
{
    Task.Run(() =>
    {
        for (global::System.Int32 j = 0; j < 100000; j++)
        {
            count++;
        }
    });
}

await Task.Delay(5000);
Console.WriteLine($"count: {count}");

